package com.zhang.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;


@Configuration
public class SecurityConfig1 extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;//自己的实现类

    //注入数据源
    @Autowired
    private DataSource dataSource;

    //配置对象
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        // jdbcTokenRepository.setCreateTableOnStartup(true); //自动创建表
        return jdbcTokenRepository;
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Bean
    PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin() //自定义自己的登录页面
                .loginPage("/login.html") //登录页面设置
                .loginProcessingUrl("/user/login") //登录访问路径
                //.defaultSuccessUrl("/test/index").permitAll() // 登录成功之后跳转的路径
                .defaultSuccessUrl("/success.html").permitAll() // 修改登录成功之后跳转的路径
                .and().authorizeRequests()//定义需要登录认证的页面
                .antMatchers("/", "/test/hello", "/user/login").permitAll()//不需要认证保护的页面
                //当前登录的用户,只有具有admins权限的,才能访问此路径
                //.antMatchers("/test/index").hasAuthority("admins,manager")      //权限

                .antMatchers("/test/index").hasAnyAuthority("admins","manager")   // 权限列表
                // .antMatchers("/test/index").hasRole("sale")  //添加限制sale角色
                //.antMatchers("/test/index").hasAnyRole("admins", "role1")  //添加限制sale角色
                .anyRequest().authenticated();

                //remember me功能
                //.and().rememberMe().tokenRepository(persistentTokenRepository()).tokenValiditySeconds(5)//有效时长
                //.userDetailsService(userDetailsService)

                //.and().csrf().disable();//关闭csrf防护,后面再讲,注释掉就是开启了



        //配置没有权限时的自定义页面
        http.exceptionHandling().accessDeniedPage("/unauth.html");

        //配置退出登录后的地址
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/hello").permitAll();
    }
}
